const mongoose = require('mongoose');
const logger = require('../utils/logger');
const config = require('./default');

// 数据库连接函数
const connectDB = async () => {
  console.log(`尝试连接到 MongoDB: ${config.mongoURI}`)
  try {
    await mongoose.connect(config.mongoURI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    logger.info('MongoDB 连接成功');
  } catch (err) {
    logger.error('MongoDB 连接失败:', err.message);
    // 连接失败时退出进程
    process.exit(1);
  }
};

module.exports = connectDB; 